package com.winsomely.leecode;

import java.util.Arrays;

/**
 * 加一
 * 
 * @author liujiawei
 *
 */
public class PlusOne {

	public int[] plusOne(int[] digits) {
		int i = digits.length - 1;
		boolean isOver = false;
		do {
			digits[i] = digits[i] + 1;
			if (digits[i] >= 10) {
				digits[i] = 0;
				if (i <= 0) {
					isOver = true;
				}
				i--;
			} else {
				break;
			}
		} while (i >= 0);
		if (isOver) {
			int[] newArray = new int[digits.length + 1];
			newArray[0] = 1;
			System.arraycopy(digits, 0, newArray, 1, digits.length);
			return newArray;
		}
		return digits;
	}

	public static void main(String[] args) {
		PlusOne t = new PlusOne();

		int[] digits = { 9, 0, 0, 9, 9 };
		// int[] ss = new int[4];
		// ss[0] = 1;
		// System.arraycopy(digits, 0, ss, 1, digits.length);
		int[] ss = t.plusOne(digits);
		for (int s : ss) {
			System.out.print(s);
		}

	}

}
